package gr.unipi.mscd1;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.snmp4j.PDU;
import org.snmp4j.smi.VariableBinding;

public class InterfaceInfoDetails extends SnmpInfo {
	
	private int ifSpeed;
	
	private Calendar initTime;
	
	public InterfaceInfoDetails(SimpleSnmpClient client, int ifSpeed, String suffix) {
		super(client, suffix);
		this.ifSpeed = ifSpeed;
		initialize();
	}
	
	private final ArrayList<OidInfo> OIDs = new ArrayList<OidInfo>() {
		
		private static final long serialVersionUID = 1L;

		{
			add( new OidInfo(OidConsts.ifInOctets,         true, ParamType.LONG) );  //0
			add( new OidInfo(OidConsts.ifOutOctets,        true, ParamType.LONG) );  //1
			add( new OidInfo(OidConsts.ifInUcastPkts,      true, ParamType.LONG) );  //2
			add( new OidInfo(OidConsts.ifOutUcastPkts,     true, ParamType.LONG) );  //3
			add( new OidInfo(OidConsts.ifInNUcastPkts,     true, ParamType.LONG) );  //4
			add( new OidInfo(OidConsts.ifOutNUcastPkts,    true, ParamType.LONG) );  //5
			add( new OidInfo(OidConsts.ifInMulticastPkts,  true, ParamType.LONG) );  //6
			add( new OidInfo(OidConsts.ifOutMulticastPkts, true, ParamType.LONG) );  //7
			add( new OidInfo(OidConsts.ifInBroadcastPkts,  true, ParamType.LONG) );  //8
			add( new OidInfo(OidConsts.ifOutBroadcastPkts, true, ParamType.LONG) );  //9
			add( new OidInfo(OidConsts.ifInDiscards,       true, ParamType.LONG) );  //10
			add( new OidInfo(OidConsts.ifOutDiscards,      true, ParamType.LONG) );  //11
			add( new OidInfo(OidConsts.ifInErrors,         true, ParamType.LONG) );  //12
			add( new OidInfo(OidConsts.ifInUnknownProtos,  true, ParamType.LONG) );  //13
			add( new OidInfo(OidConsts.ifOutErrors,        true, ParamType.LONG) );  //14
		}
		
	};
	
	@Override
	protected List<OidInfo> getOids() {
		return OIDs;
	}
	
	@Override
	public void start() {
		initTime = Calendar.getInstance();
		super.start();
	}
	
	private double getPassedSeconds() {
		Calendar nowTime = Calendar.getInstance();
		return ((double)(nowTime.getTimeInMillis() - initTime.getTimeInMillis())) / 1000.0;
	}

	public int getIfSpeed() {
		return ifSpeed;
	}

	public long getCurrIfInOctets() {
		return (Long)OIDs.get(0).getCurrValue();
	}

	public long getCurrIfOutOctets() {
		return (Long)OIDs.get(1).getCurrValue();
	}

	public long getCurrIfInUcastPkts() {
		return (Long)OIDs.get(2).getCurrValue();
	}

	public long getCurrIfOutUcastPkts() {
		return (Long)OIDs.get(3).getCurrValue();
	}

	public long getCurrIfInNUcastPkts() {
		return (Long)OIDs.get(4).getCurrValue() + (Long)OIDs.get(6).getCurrValue() + (Long)OIDs.get(8).getCurrValue();
	}

	public long getCurrIfOutNUcastPkts() {
		return (Long)OIDs.get(5).getCurrValue() + (Long)OIDs.get(7).getCurrValue() + (Long)OIDs.get(9).getCurrValue();
	}
	
	public long getCurrIfInDiscards() {
		return (Long)OIDs.get(10).getCurrValue() + (Long)OIDs.get(12).getCurrValue() + (Long)OIDs.get(13).getCurrValue();
	}
	
	public long getCurrIfOutDiscards() {
		return (Long)OIDs.get(11).getCurrValue() + (Long)OIDs.get(14).getCurrValue();
	}
	
	
	
	public double getAvgIfInOctets() {
		return ((double)(Long)OIDs.get(0).getAvgValue()) / getPassedSeconds();
	}

	public double getAvgIfOutOctets() {
		return ((double)(Long)OIDs.get(1).getAvgValue()) / getPassedSeconds();
	}

	public double getAvgIfInUcastPkts() {
		return ((double)(Long)OIDs.get(2).getAvgValue()) / getPassedSeconds();
	}

	public double getAvgIfOutUcastPkts() {
		return ((double)(Long)OIDs.get(3).getAvgValue()) / getPassedSeconds();
	}

	public double getAvgIfInNUcastPkts() {
		long v = (Long)OIDs.get(4).getAvgValue() + (Long)OIDs.get(6).getAvgValue() + (Long)OIDs.get(8).getAvgValue();
		return ((double)v) / getPassedSeconds();
	}

	public double getAvgIfOutNUcastPkts() {
		long v = (Long)OIDs.get(5).getAvgValue() + (Long)OIDs.get(7).getAvgValue() + (Long)OIDs.get(9).getAvgValue();
		return ((double)v) / getPassedSeconds();
	}
	
	public double getAvgIfInDiscards() {
		long v = (Long)OIDs.get(10).getAvgValue() + (Long)OIDs.get(12).getAvgValue() + (Long)OIDs.get(13).getAvgValue();
		return ((double)v) / getPassedSeconds();
	}
	
	public double getAvgIfOutDiscards() {
		long v = (Long)OIDs.get(11).getAvgValue() + (Long)OIDs.get(14).getAvgValue();
		return ((double)v) / getPassedSeconds();
	}
}
